﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using WebLog;

namespace Data
{
    public sealed class DataBase
    {

        private SqlConnection connection = null;
        private SqlCommand command = null;
        private SqlTransaction transaction = null;
        private string connectionString;
        private static DbProviderFactory factory = null;

        public DataBase()
        {
            Configure();
        }


        private void Configure()
        {
            try
            {
                connectionString = ConfigurationManager.ConnectionStrings["VotoOnline"].ConnectionString;
                DataBase.factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
            }
            catch (ConfigurationException ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
            }
        }


        public void Disconnect()
        {
            if (connection.State.Equals(ConnectionState.Open))
            {
                connection.Close();
            }
        }

        public void Connect()
        {
            if (connection != null && !connection.State.Equals(ConnectionState.Closed))
            {
                Exception ex = new ApplicationException("Conexion Abierta");
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
            }
            try
            {
                if (this.connection == null)
                {
                    connection = (SqlConnection)factory.CreateConnection();
                    connection.ConnectionString = connectionString;
                }
                connection.Open();
            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
            }
        }


        public void ProcedureSqlName(string storedProcedure)
        {
            command = (SqlCommand)factory.CreateCommand();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = storedProcedure;
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
        }


        public void AssignNull(string name)
        {
            AssignParameter(name, null);
        }


        public void AssignString(string name, string value)
        {
            AssignParameter(name, value);
        }


        public void AssignDecimal(string name, decimal value)
        {
            AssignParameter(name, value);
        }


        public void AssignInt(string name, int value)
        {
            AssignParameter(name, value.ToString());
        }
       
        public void AssignDateTime(string name, DateTime value)
        {
            AssignParameter(name, value);
        }


        public void AssignGuid(string name, Guid value)
        {
            AssignParameter(name, value);
        }


        public void AssignLong(string name, long value)
        {
            AssignParameter(name, value.ToString());
        }


        public void AssignBool(string name, bool value)
        {
            AssignParameter(name, value);
        }


        public void AssignFloat(string name, float value)
        {
            AssignParameter(name, value);
        }


        private void AssignParameter(string name, object value)
        {
            command.Parameters.AddWithValue(name, value);
        }


        public DbDataReader ExecuteDataReader()
        {
            return command.ExecuteReader();
        }


        public int ExecuteScalar()
        {
            int escalar = 0;
            try
            {
                escalar = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
            }
            return escalar;
        }


        public int ExecuteNonQuery()
        {
            return command.ExecuteNonQuery();
        }


        public void BeginTransaction()
        {
            if (transaction == null)
            {
                transaction = connection.BeginTransaction();
            }
        }


        public void RollbackTransaction()
        {
            if (transaction != null)
            {
                transaction.Rollback();
            }
        }


        public void CommitTransaction()
        {
            if (transaction != null)
            {
                transaction.Commit();
            }
        }

        public void ReleaseResources()
        {
            connection.Dispose();
        }
    }
}
